package com.woniuxy.education.controller;

import com.woniuxy.education.entity.Class;
import com.woniuxy.education.entity.*;
import com.woniuxy.education.service.StuAccountManageService;
import com.woniuxy.education.service.StudentService;
import com.woniuxy.education.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/student_account_management")
public class StuAccountManageController extends ExceptionCenter{
    @Autowired
    StudentService studentService;
    @Autowired
    StuAccountManageService stuAccountManageService;
    //添加学生和学生账户
    @Transactional
    @ResponseBody
    @RequestMapping("/addStuUser")
    public ResponseResult addStusUser(Student student){
        ResponseResult result = new ResponseResult<>();
        User user = new User();
        if(stuAccountManageService.queryUserByAccount(student.getNumber()+"") == null){
            user.setAccount(student.getNumber()+"");
            user.setPassword("123456");
            user.setType_id(0);
            stuAccountManageService.addUser(user);
            user = stuAccountManageService.queryUserByAccount(user.getAccount());
            student.setU_id(user.getId());
            stuAccountManageService.addStudent(student);
            result.setCode(200);
            result.setMessage("操作成功");
        }else{
            result.setCode(201);
            result.setMessage("学生账号或已存在");
        }
        return  result;
    }
    //查询特定学生（模糊名字）
    @ResponseBody
    @RequestMapping("/queryStudent")
    public PageResult<List<Student>> queryStudent(int c_id, int page, int size){
        return stuAccountManageService.findStudent(c_id,page,size);
    }
    //模糊查询具体学生（根据学生名字）
    @ResponseBody
    @RequestMapping("/searchStu")
    public PageResult<List<Student>> searchStu(int c_id, int page, int size,String name){
        return stuAccountManageService.searchStu(c_id,page,size,name);
    }
    //查询具体班级用过c_id
    @ResponseBody
    @RequestMapping("/searchClassByC_id")
    public ResponseResult searchClassByC_id(Integer c_id){
        ResponseResult result = new ResponseResult<>();
        Class clazz = stuAccountManageService.searchClassByC_id(c_id);
        result.setData(clazz);
        return result;
    }
    //获取所有班级列表
    @ResponseBody
    @RequestMapping("/getAllClass")
    public ResponseResult getAllClass(){
        ResponseResult result = new ResponseResult<>();
        result.setRights(stuAccountManageService.getAllClass());
        return result;
    }
    //重置密码
    @ResponseBody
    @RequestMapping("/resetPassword")
    public ResponseResult resetPassword(Integer u_id){
        ResponseResult result = new ResponseResult<>();
        User user = stuAccountManageService.queryUserByU_id(u_id);
        if("123456".equals(user.getPassword())){
            result.setCode(201);
            result.setMessage("已是重置密码，无需重置操作");
        }else{
        stuAccountManageService.resetPassword(u_id);
        result.setCode(200);
        result.setMessage("操作成功");
        }
        return  result;
    }
    //查询转班信息
    @ResponseBody
    @RequestMapping("/queryStuChangeClassByC_id")
    public ResponseResult queryStuChangeClass(Integer c_id,String state){
        ResponseResult result = new ResponseResult<>();
        List<Change_StuClass> change_stuClasses =  stuAccountManageService.queryStuChangeClass(c_id,Integer.parseInt(state));
        if(change_stuClasses != null){
            result.setCode(200);
            result.setRights(change_stuClasses);
        }
        return  result;
    }
    //查询用户，通过u_id
    @ResponseBody
    @RequestMapping("/queryUser")
    public ResponseResult queryUserByU_id(Integer u_id){
        ResponseResult result = new ResponseResult<>();
        User user = stuAccountManageService.queryUserByU_id(u_id);
        result.setCode(200);
        result.setRights(user);
        return  result;
    }
    //转班申请
    @ResponseBody
    @RequestMapping("/changeStuClass")
    public ResponseResult changeStuClass(Integer number,Integer nc_id,Integer ac_id,String reason){
        ResponseResult result = new ResponseResult<>();
        //验证学生是否存在
        Student student = stuAccountManageService.queryStudentByNumber(number);
        if(student == null){
            result.setCode(201);
            result.setMessage("该学生不存在");
            return  result;
        }
        //
        Change_StuClass change_StuClass1 = stuAccountManageService.queryStuChangClassByS_id(student.getId());
        if(change_StuClass1 != null && change_StuClass1.getState() == 0){
            result.setCode(201);
            result.setMessage("该学生已存在转班申请");
            return  result;
        }
        Change_StuClass change_StuClass = new Change_StuClass();
        Class class1 = stuAccountManageService.searchClassByC_id(nc_id);
        Class class2 = stuAccountManageService.searchClassByC_id(ac_id);
        Teacher petteacher = stuAccountManageService.queryTeacherByName(class1.getHeadTeacher());

        Teacher confteacher = stuAccountManageService.queryTeacherByName(class2.getHeadTeacher());

        change_StuClass.setStudent(student);
        change_StuClass.setState(0);
        Calendar cd = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = formatter.format(cd.getTime());
        change_StuClass.setApplication_date(format);
        change_StuClass.setClass1(class1);
        change_StuClass.setClass2(class2);
        change_StuClass.setPetteacher(petteacher);
        change_StuClass.setConfteacher(confteacher);
        change_StuClass.setReason(reason);
        stuAccountManageService.addChangeStuClass(change_StuClass);
        result.setCode(200);
        result.setMessage("操作成功，等待目标班级班主任确认");
        return  result;
    }
    //撤回消息
    @ResponseBody
    @RequestMapping("/retractStuChangeClass")
    public ResponseResult retractStuChangeClass(Integer id){
        ResponseResult result = new ResponseResult<>();

        Calendar cd = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = formatter.format(cd.getTime());
        //将状态设为1
        Boolean bl = stuAccountManageService.updateStuChangClassById(1,id,format);
        if(bl) {
            result.setCode(200);
            result.setMessage("操作成功");
            return result;
        }else{
            result.setCode(201);
            result.setMessage("操作失败");
            System.out.println(3);
            return result;
        }
    }
    //拒绝申请
    @ResponseBody
    @RequestMapping("/refuseStuChangeClass")
    public ResponseResult refuseStuChangeClass(Integer id){
        ResponseResult result = new ResponseResult<>();

        Calendar cd = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = formatter.format(cd.getTime());
        //将状态设为1
        Boolean bl = stuAccountManageService.updateStuChangClassById(3,id,format);
        if(bl) {
            result.setCode(200);
            result.setMessage("操作成功");
            return result;
        }else{
            result.setCode(201);
            result.setMessage("操作失败");
            System.out.println(3);
            return result;
        }
    }
    //同意学生转班
    @Transactional
    @ResponseBody
    @RequestMapping("/aggreeStuChangeClass")
    public ResponseResult aggreeStuChangeClass(Integer id){
        ResponseResult result = new ResponseResult<>();
        Change_StuClass change_StuClass = stuAccountManageService.queryStuChangClassById(id);
        Student student = change_StuClass.getStudent();
        student.setC_id(change_StuClass.getClass2().getId());
        Calendar cd = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = formatter.format(cd.getTime());
        Boolean b1 = stuAccountManageService.updateStudentClass(student.getId(),student.getC_id());
        //将状态设为2
        Boolean b2 = stuAccountManageService.updateStuChangClassById(2,id,format);
        if(b2 == true && b1 == true) {
            result.setCode(200);
            result.setMessage("操作成功");

            return result;
        }else{
            result.setCode(201);
            result.setMessage("操作失败");
            System.out.println(3);
            return result;
        }
    }

    //查询一个年级的所有班
    @ResponseBody
    @RequestMapping("/getClassByG_id")
    public ResponseResult getClassByG_id(Integer g_id){
        ResponseResult result = new ResponseResult<>();
        List<Class> classes = stuAccountManageService.getClassByG_id(g_id);
        result.setCode(200);
        result.setData(classes);
        return  result;
    }
}
